/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 解析.node.node;

import 解析.node.others.TreeInfo;
import 解析.Matcher;
import 解析.Pattern;

/**
 *
 * @author yanxi
 */
public class First extends Node {

    public Node atom;

    First(Node node) {
        this.atom = BnM.optimize(node);
        getChildren().add(node);
    }

    @Override
    public boolean match(Matcher matcher, int i, CharSequence seq) {
        if (atom instanceof BnM) {
            return atom.match(matcher, i, seq)
                    && next.match(matcher, matcher.last, seq);
        }
        for (;;) {
            if (i > matcher.to) {
                matcher.hitEnd = true;
                return false;
            }
            if (atom.match(matcher, i, seq)) {
                return next.match(matcher, matcher.last, seq);
            }
            i += Pattern.countChars(seq, i, 1);
            matcher.first++;
        }
    }

    @Override
    public boolean study(TreeInfo info) {
        atom.study(info);
        info.maxValid = false;
        info.deterministic = false;
        return next.study(info);
    } 
}
